﻿using Kvaser.CanLib;

namespace CANSendDemo
{
    internal class Program
    {
        private static int count = 0;
        static void Main(string[] args)
        {
            //初始化Can对象
            Canlib.canInitializeLibrary();
            //打开通道，获取句柄
            int handle = Canlib.canOpenChannel(0, Canlib.canOPEN_ACCEPT_VIRTUAL);
            //通过句柄设置波特率
            Canlib.canSetBitrate(handle, Canlib.canBITRATE_500K);
            //打开连接
            Canlib.canBusOn(handle);
            while (count < 100)
            {
                //发送
                byte[] send_dates = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
                //句柄，消息标识符，数据，数据位，帧类型
                Canlib.canWrite(handle, 456, send_dates, 8, Canlib.canMSG_STD);
                Console.WriteLine($"Send : {BitConverter.ToString(send_dates)}, Id : {456.ToString()}");
                Thread.Sleep(1000);
                count++;
            }

            //关闭连接
            Canlib.canBusOff(handle);
            Console.WriteLine("按任意键退出......");
            Console.ReadKey();
        }
    }
}
